Segmentation. strcmp [C]

Posted by FILIaS on Stack Overflow See other posts from Stack Overflow or by FILIaS
Published on 2011-01-16T12:28:17Z Indexed on 2011/01/16 12:53 UTC
Read the original article Hit count: 383

Filed under:
|
|

Hello, I have a file with format: [name][number][amount] number is taken as a string. and im using it in a strcmp. Problem is that i get a segmentation fault. I know that on most cases when strcmp signs segmentation fault it means that one of the parameters is null or cant find its "end" ('\0'). I checked with gdb and i cant say if this is the problem.Take a look:

> (gdb) bt full
> #0  0x08048729 in lookup (hashtable=0x804b008, hashval=27, 
>     number=0x804b740 "6900101001") 
>         list = 0xffffffff
> #1  0x080487ac in add (hashtable=0x804b008, 
>     number=0x804b740 "9900101001", name=0x804b730 "Smithpolow",
> time=6943) 
>         new_elem = 0xffffffff
>         hashval = 27
> #2  0x08048b25 in main (argc=1, argv=0xbffff4b4) 
>         number = 0x804b740 "9900101001"
>         name = 0x804b730 "Smithpolow"
>         time = 6943
>         i = 2

Code:

clientsList *lookup_on_Clients(clientsHashTable *hashtable,int hashval,char number[10])
{
 printf("NUMBER:%s\n",number);
 clientsList *list=hashtable[hashval].head;
 for(list; list!=NULL; list=list->next){
   if (strcmp(number,list->number)==0)   //SEGMENTATION!
    return list;

 }
 return NULL;
}


int add ( HashTable* hashtable,char number[10],char* name,int time)
{
     List *new_elem;
    int hashval=hash (hashtable,number);

    new_elem=hashtable[hashval].head;
    if(hashtable[hashval].length>0)
    {                   
          if ((lookup (hashtable,hashval,number))!=NULL) {return 0;}    
    }
    //an den uparxei stoixeio sth lista
    if (!(new_elem=malloc(sizeof(struct clientsList)))){ return -1;}

    //insert values for the new elem
    new_elem->number=strdup(number);    
    new_elem->name=strdup(name);
    new_elem->time=time;

    hashtable[hashval].head=new_elem;
    new_elem->next=NULL;
    hashtable[hashval].length++;

    /* rehash existing entries if necessary */
    if(hashTableSize(hashtable)>= 2*primes[PrimesIndex])    
    {    
         hashtable = expand(hashtable);
         if (hashtable ==NULL){
           return 0;
         }
         PrimesIndex++;
    }

    return 1;
}

and the main:

FILE * File2;
    if ( ( File2=fopen(" File.txt","r")) !=NULL ) 
    { // File.txt format:  [name number time] e.g lountemis 6900254565 700651
        int li = 0;
        char *lin = (char *) malloc(MAX_LINE * sizeof(char));

        while(fgets(lin, MAX_LINE, clientFile2) != NULL)
        {
            token = my_linetok(lin, " ");
            if(token != NULL)
            {
  char* number ;
  char* name;
  int time;
  int i;
                for(i = 0; token[i] != NULL; i++)
                {
   name=strdup(token[0]);
   number=strdup(token[1]);
   time=atoi(token[2]);

   if (i==2)
   { int insertDone=0;
         insertDone =add(my_hash_table,number,name,time);   

   } 
  }
  free(name); 
  free(number);
  free(token);

            }
            else 
     {
                printf("Error reading line %s\n", lin);
                exit(1);   
            }
        }

    } 
    else 
    {
        printf("Error opening file \nEXIT!");
 exit(0);
    }

© Stack Overflow or respective owner

Related posts about c

    Related posts about segmentation-fault